<?php
if(!isset($MAIN)){
	include_once("includes/class.ibnfm.php");
	$MAIN = new MAIN();
}
if(isset($_REQUEST['logout'])){
    $MAIN->users->logout();
    exit();
}
if(isset($_POST['txt_email'])){
	$chk=1;
	//if(isset($_REQUEST['chk_remember'])){$chk=1;}
	if(!$MAIN->validEmail($_REQUEST['txt_email']) || $_REQUEST['txt_pass']==""){
		?>
		<script type="text/javascript">
			sendInfo($('general_info'),'Mailadress och lösenord saknas!');
		</script>
		<?php
	}elseif(!$MAIN->users->doLogin($_REQUEST['txt_email'], $_REQUEST['txt_pass'], $chk)){
		?>
		<script type="text/javascript">
        	sendInfo($('general_info'),'Felaktigt mailadress eller lösenord!');
		</script>
		<?php
		exit();
	}else{        
		?>
		<script type="text/javascript">document.location.href='index.php';</script>
		<?php
	}
	exit();
}
if(isset($_REQUEST['txt_name_forgot'])){
	$email = $_REQUEST['txt_name_forgot'];
	if($MAIN->validEmail($email)){		
		$sql = sprintf("select * from `".tblPrefix."users` where `email`='%s'", mysql_escape_string($email));
		$req = mysql_query($sql) or die('forgotPassword error : '.mysql_error().'  '.$sql);
		if(mysql_num_rows($req)>0){
			$row = mysql_fetch_assoc($req);
			$subject = "Inloggningsuppgifter har skickats till ".$email;
			//Message body
			$message = "Hej!<br/><br/>
			Det verkar som att du har glömt dina inloggningsuppgifter.<br/>
			Det gör inget, det är många inloggningsuppgifter du behöver ta reda på hela tiden, så här<br/>
			kommer lite hjälp!<br/>
			Dina inloggninsuppgifter för ibnrushd.eu är :<br/><br/>
			Användarnamn : ".$row['email']."<br/>
			Lösenord : ".$MAIN->decode($row['drowp'])."<br/><br/>
			Lycka till,<br/>
			/ibnrushd.eu";
			$MAIN->sendMail($email, $subject, utf8_decode($message));
			?>
			<script type="text/javascript">			
				sendInfo($('info_forgot'),'Inloggningsuppgifter skickas!');
				(function(){document.location.href='index.php';}).delay(4000);
			</script>
			<?php
		}else{
			?>
			<script type="text/javascript">
				sendInfo($('info_forgot'),'Mailadressen är inte registrerat i databasen !');				
				$('txt_name_forgot').innerHTML='';
				$('txt_name_forgot').focus();
			</script>
			<?php
		}
	}else{
	?>
		<script type="text/javascript">			
			sendInfo($('info_forgot'),'Mailadress är inte valid!');
			$('txt_name_forgot').innerHTML='';
			$('txt_name_forgot').focus();
		</script>
	<?php
	}
	exit();
}
if(isset($_REQUEST['forgot'])){
?>
<form name="frm_forgot" id="frm_forgot" action="login.php" method="post">
	<h2 style="font-size:12px;">Glömt dina inloggningsuppgifter?</h2>	
	<input style="position:relative;" type="text" id="txt_name_forgot" name="txt_name_forgot" value=""/>
	<table cellpadding="0" cellspacing="0" style="width:100%;">
		<tr>
			<td style="text-align:right;"><input type="button" name="btn_send" value="Sckika" onclick="ajaxPost($('frm_forgot'),'login.php');"/></td>
		</tr>
	</table>		
	
</form>
<?php
exit();
}

if(!$MAIN->users->checkLogin(false)){
?>
<div id="login">
	<div id="btn_login" class="btn_login">
		<h3>Inloggning</h3>		
	</div>
	
	<div id="details_login" class="frm_login">
		<form name="frm_login" id="frm_login" action="login.php" method="post">
			<input type="text" id="txt_email" name="txt_email" value="mailadress"/><br/>
			<input type="password" id="txt_pass" name="txt_pass" value="lösenord"/>
			<input type="hidden" name="chk_remember" id="chk_remember" value="1"/>						
			<table cellpadding="0" cellspacing="0">				
				<tr>
					<td style="text-align:right;"><a class="btn" href="javascript:sendForm();">Logga in</a></td>
				</tr>
				<tr>
					<td style="text-align:right;"><a class="forgot" href="javascript:void(0);" onclick="ajaxGet('login.php','forgot',$('login'));">Glömt inloggningsuppgifter?</a></td>
				</tr>
			</table>
		</form>
	</div>
</div>
<script type="text/javascript">
$('txt_email').addEvents({
	'focus':function(){
		if(this.value=='mailadress'){this.value='';}
	},
	'blur':function(){
		if(this.value==''){this.value='mailadress';}
	}
});
$('txt_pass').addEvents({
	'focus':function(){
		if(this.value=='lösenord'){this.value='';}
	},
	'blur':function(){
		if(this.value==''){this.value='lösenord';}
	}
});
$('frm_login').addEvent('keydown',function(event){	
	if(event.key=='enter'){
		sendForm();
	}
});
var sendForm = function(){
	ajaxPost($('frm_login'),'login.php');
}
</script>
<?php
}else{
?>
<div id="login">
	<div id="btn_login" class="btn_login">
		<h3>Hej <?php if($MAIN->currentUser['firstname']!=""){echo($MAIN->currentUser['firstname']);}else{echo($MAIN->currentUser['email']);}?></h3>		
	</div>
	
	<div id="details_login" class="frm_login">
		<form name="frm_logout" id="frm_logout" action="login.php" method="post">
			<input type="hidden" id="logout" name="logout" value="true"/><br/>
			<input type="submit" name="btn_logout" id="btn_logout" value="Logga Ut"/>
		</form>
	</div>
</div>
<?php
}
?>